package com.societyGames.codeAlong.state.codeExercise.model
{
import com.societyGames.codeAlong.state.codeExercise.model.link.IHyperText;
import com.societyGames.codeAlong.state.codeExercise.model.link.ILinkable;

public class ClassData implements ILinkable, IHyperText
{
  public var name:String;
  public var description:String;
  public var example:String;
  public var fields:Vector.<FunctionParameterData> = new Vector.<FunctionParameterData>();
  public var methods:Vector.<FunctionData> = new Vector.<FunctionData>();
  public var isGlobal:Boolean;

  private static const _classId:String = "class";

  //IHyperText-->
  public function toHyperText():String
  {
    //Display signature
    var text:String = "<b>" + name + "</b><br>" + description;
    return text;
  }

  //<--IHyperText

  //ILinkable-->
  public function toLink():String
  {
    return _classId + LibraryData.separator + this.name;
  }

  //<--ILinkable

  public static function fromXml(xml:XML):ClassData
  {
    var classData:ClassData = new ClassData();

    //Get attributes
    classData.name = xml.@name;
    classData.description = xml.@description;
    classData.isGlobal = xml.hasOwnProperty("@isGlobal") && xml.@isGlobal == "true";

    //Handle fields
    for each (var fieldXml:XML in xml.field)
    {
      classData.fields.push(FunctionParameterData.fromXml(classData, fieldXml));
    }

    //Handle arguments
    for each (var methodXml:XML in xml.method)
    {
      classData.methods.push(FunctionData.fromXml(classData, methodXml));
    }

    //Get the example if there is one.
    if ("example" in xml)
    {
      classData.example = xml.example[0].toString();
    }

    //The newly created
    return classData;
  }
}
}